package com.aj.activity.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.activity.R;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.ManagerChildRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;

public class NewChildBigFragment extends FamilyBaseFragment {
	private static final String TAG = NewChildBigFragment.class.getName();
	private TextView alert_text;
	private EditText enter_lovehomenumber;
	private EditText enter_telphone;
	private EditText enter_description;
	private RadioGroup kinds_radiogroup;
	private Button sure_button;
	private Button cannel_button;
	private String isPravite = "1";
	private UserDao userDao;
	private User user;
	private String callId = "1";

	private String sex = "0";
	private String callName = "爸爸";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_big_child, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		enter_lovehomenumber = (EditText) view
				.findViewById(R.id.enter_lovehomenumber);
		enter_telphone = (EditText) view.findViewById(R.id.enter_telphone);
		enter_description = (EditText) view
				.findViewById(R.id.enter_description);
		kinds_radiogroup = (RadioGroup) view
				.findViewById(R.id.kinds_radiogroup);
		alert_text = (TextView) view.findViewById(R.id.alert_text);
		callName = getActivity().getIntent().getStringExtra("callName");
		if (callName != null) {
			alert_text.setText(String.format(
					getString(R.string.alert_text_string), callName));
			if (callName.equals("爸爸")) {
				callId = "1";
			} else if (callName.equals("妈妈")) {
				callId = "2";
			} else if (callName.equals("儿子")) {
				sex = "0";
				callId = "7";
			} else if (callName.equals("女儿")) {
				sex = "1";
				callId = "8";
			} else if (callName.equals("胎儿")) {
				callId = "13";
			}
		}
		userDao = new UserDao(getActivity());
		user = userDao.getCurrentUser(LoveHomeApplication.token);
		ButtonClick buttonClick = new ButtonClick();
		sure_button = (Button) view.findViewById(R.id.sure_button);
		cannel_button = (Button) view.findViewById(R.id.cannel_button);
		sure_button.setOnClickListener(buttonClick);
		cannel_button.setOnClickListener(buttonClick);
		kinds_radiogroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.familypublic:
							isPravite = "1";
							break;
						case R.id.familyprivate:
							isPravite = "0";
							break;
						}
					}
				});

	}

	class ButtonClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.cannel_button:
				getActivity().finish();
				break;
			case R.id.sure_button:

				if (!enter_lovehomenumber.getText().toString().trim()
						.equals("")
						&& enter_telphone.getText().toString().trim()
								.equals("")) {
					Toast.makeText(getActivity(), "请输入爱家号或手机号~",
							Toast.LENGTH_LONG).show();
					return;
				}
				showProgressDialog();
				new ManagerChildRequest().excute(getActivity(),
						user.getUser_id(), "1", null, "0", null, null, null,
						sex, null, enter_telphone.getText().toString(),
						isPravite, enter_lovehomenumber.getText().toString(),
						callId, enter_description.getText().toString(),
						new IHttpCallBack() {
							@Override
							public void success(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										getActivity().finish();
									}
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});

				break;
			}
		}
	}
}